From 8935293c66cfa6f7d9fa4d0c5d1d3b74c0cbc7db Mon Sep 17 00:00:00 2001 From: Jyrki Gadinger Date: Thu, 24 Apr 2025 18:01:54 +0200 Subject: [PATCH] refactor: add new filePath method to folder Signed-off-by: Jyrki Gadinger --- src/gui/folder.cpp | 20 ++++++++++++++++++++ src/gui/folder.h | 8 ++++++++ src/gui/tray/activitylistmodel.cpp | 16 +--------------- 3 files changed, 29 insertions(+), 15 deletions(-) diff --git a/src/gui/folder.cpp b/src/gui/folder.cpp index 26df7db12..41772fa70 100644 --- a/src/gui/folder.cpp +++ b/src/gui/folder.cpp @@ -998,6 +998,26 @@ void Folder::migrateBlackListPath(const QString &legacyPath) } } +QString Folder::filePath(const QString& fileName) +{ + const auto folderDir = QDir(_canonicalLocalPath); + +#ifdef Q_OS_WIN + // Edge case time! + // QDir::filePath checks whether the passed `fileName` is absolute (essentialy by using `!QFileInfo::isRelative()`). + // In the case it's absolute, the `fileName` will be returned instead of the complete file path. + // + // On Windows, if `fileName` starts with a letter followed by a colon (e.g. "A:BCDEF"), it is considered to be an + // absolute path. + // Since this method should return the file name file path starting with the canonicalLocalPath, catch that special case here and prefix it ourselves... + return fileName.length() >= 2 && fileName[1] == ':' + ? _canonicalLocalPath + fileName + : folderDir.filePath(fileName); +#else + return folderDir.filePath(fileName); +#endif +} + bool Folder::isFileExcludedAbsolute(const QString &fullPath) const { return _engine->excludedFiles().isExcluded(fullPath, path(), _definition.ignoreHiddenFiles); diff --git a/src/gui/folder.h b/src/gui/folder.h index f66d4c81b..38d288d9d 100644 --- a/src/gui/folder.h +++ b/src/gui/folder.h @@ -165,6 +165,14 @@ public: */ [[nodiscard]] QString remotePathTrailingSlash() const; + /** + * Returns the path name of a file in the local canonical path. + * + * Similar to `QDir(path()).filePath(...)`, except file names like "Z:test" + * are treated as relative paths on Windows. + */ + [[nodiscard]] QString filePath(const QString& fileName); + [[nodiscard]] QString fulllRemotePathToPathInSyncJournalDb(const QString &fullRemotePath) const; void setNavigationPaneClsid(const QUuid &clsid) { _definition.navigationPaneClsid = clsid; } diff --git a/src/gui/tray/activitylistmodel.cpp b/src/gui/tray/activitylistmodel.cpp index 9bd401859..8e6df9692 100644 --- a/src/gui/tray/activitylistmodel.cpp +++ b/src/gui/tray/activitylistmodel.cpp @@ -686,7 +686,6 @@ void ActivityListModel::slotTriggerDefaultAction(const int activityIndex) } auto folder = FolderMan::instance()->folder(activity._folder); - const auto folderDir = QDir(folder->path()); const auto fileLocation = activity._syncFileItemStatus == SyncFileItem::FileNameInvalidOnServer ? InvalidFilenameDialog::FileLocation::NewLocalFile : InvalidFilenameDialog::FileLocation::Default; @@ -694,21 +693,8 @@ void ActivityListModel::slotTriggerDefaultAction(const int activityIndex) ? InvalidFilenameDialog::InvalidMode::ServerInvalid : InvalidFilenameDialog::InvalidMode::SystemInvalid; -#ifdef Q_OS_WIN - // Edge case time! - // QDir::filePath checks whether the passed `fileName` is absolute (essentialy by using `!QFileInfo::isRelative()`). - // On Windows, if `fileName` starts with a letter followed by a colon (e.g. "A:BCDEF"), it is considered to be an - // absolute path. - // Since the complete desired path is required by InvalidFilenameDialog, catch that special case here and prefix it ourselves... - const auto filePath = activity._file.length() >= 2 && activity._file[1] == ':' - ? folder->path() + activity._file - : folderDir.filePath(activity._file); -#else - const auto filePath = folderDir.filePath(activity._file); -#endif - _currentInvalidFilenameDialog = new InvalidFilenameDialog(_accountState->account(), folder, - filePath, fileLocation, invalidMode); + folder->filePath(activity._file), fileLocation, invalidMode); connect(_currentInvalidFilenameDialog, &InvalidFilenameDialog::accepted, folder, [folder]() { folder->scheduleThisFolderSoon(); }); -- 2.30.2